package com.swjd.controller;

import com.swjd.pojo.Goods;
import com.swjd.service.GoodsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Controller
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @RequestMapping("/index")
    public String index(){
       return "findByGoodsDistrict";
    }
    @RequestMapping("/findByGoodsDistrict")
    public String findByGoodsDistrict(Integer goodsDistrict, Model model){
        if (goodsDistrict==0){
            List<Goods> byGoodsDistrict = goodsService.findAll();
            model.addAttribute("byGoodsDistrict",byGoodsDistrict);
        }else {
            Goods byGoodsDistrict = goodsService.findByGoodsDistrict(goodsDistrict);
            model.addAttribute("byGoodsDistrict", byGoodsDistrict);
        }
    return "findSuccess";
    }
    @RequestMapping("/add")
    public String add(){
        return "add";
    }
    @RequestMapping("/addGoods")
    public String add(Integer id, String goodsName, Integer goodsDistrict, float goodsPrice, Integer goodsCount, Integer billStatus){
        if (goodsPrice<0||!StringUtils.isNotBlank(goodsName)){
            return "add";
        }else {
            Timestamp creationTime = new Timestamp(new Date().getTime());
            Goods addGoods = goodsService.addGoods(id, goodsName, goodsDistrict, goodsPrice, goodsCount, billStatus, creationTime);
        }

        return "findByGoodsDistrict";
    }
}
